package com.example.communitysystem.service;

import com.example.communitysystem.mapper.UserMapper;
import com.example.communitysystem.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private UserMapper userMapper;

    //创建或更新用户
    public void createOrUpdate(User user){
        User dbUser = userMapper.findByAccountId(user.getAccountId());
        if(dbUser == null){
            //插入用户
            user.setGmtCreate(System.currentTimeMillis());
            user.setGmtModified(user.getGmtCreate());
            userMapper.insertUser(user);
        }else {
            //存在用户，更新用户信息
            dbUser.setGmtModified(System.currentTimeMillis());
            dbUser.setAvatarUrl(user.getAvatarUrl());
            dbUser.setName(user.getName());
            dbUser.setToken(user.getToken());
            dbUser.setLoginName(user.getLoginName());
            userMapper.updateUser(dbUser);
        }
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}
